package com.ecoolex.ying.space.admin.graphql;

import static com.ecoolex.framework.web.api.graphql2.annotation.GraphQL2Module.Type.Admin;

import java.util.List;

import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.lang.NonNull;

import com.ecoolex.framework.common.enums.ResultCode;
import com.ecoolex.framework.common.pojo.Page;
import com.ecoolex.framework.common.pojo.Pagination;
import com.ecoolex.framework.common.pojo.Sorting;
import com.ecoolex.framework.common.util.Assert;
import com.ecoolex.framework.common.util.Check;
import com.ecoolex.framework.modularize.api.auth.admin.AdminMemberAuthority;
import com.ecoolex.framework.modularize.api.auth.annotation.AdminAuthoritativeRegister;
import com.ecoolex.framework.modularize.api.auth.user.ConsumerMember;
import com.ecoolex.framework.modularize.api.auth.user.ConsumerMemberManager;
import com.ecoolex.framework.modularize.api.service.storage.StorageInfo;
import com.ecoolex.framework.modularize.api.service.storage.StorageService;
import com.ecoolex.framework.web.api.graphql2.annotation.GraphQL2Module;
import com.ecoolex.framework.web.api.graphql2.annotation.MutationField;
import com.ecoolex.framework.web.api.graphql2.annotation.QueryField;
import com.ecoolex.framework.web.api.graphql2.annotation.Source;
import com.ecoolex.framework.web.api.graphql2.annotation.TypeField;
import com.ecoolex.ying.space.admin.core.AdminConstant;
import com.ecoolex.ying.space.entity.EstateProjectActivityGoodsOrder;
import com.ecoolex.ying.space.entity.EstateProjectActivityGoodsOrderCustomer;
import com.ecoolex.ying.space.entity.EstateProjectActivityGoodsOrderDetail;
import com.ecoolex.ying.space.entity.EstateProjectActivityGoodsOrderRefundAudit;
import com.ecoolex.ying.space.enums.EstateProjectActivityGoodsOrderRefundAuditStatus;
import com.ecoolex.ying.space.pojo.UpdateEstateProjectActivityGoodsOrderCustomerDTO;
import com.ecoolex.ying.space.ws.EstateProjectActivityGoodsOrderService;

@GraphQL2Module(type = Admin, description = "地产项目活动商品订单管理")
@AdminAuthoritativeRegister(parentKey = AdminConstant.AUTH_MODULE_KEY, key = EstateProjectActivityGoodsOrderGraphql.AUTH, name = "地产项目活动商品订单管理")
public class EstateProjectActivityGoodsOrderGraphql {

	public static final String AUTH = AdminConstant.AUTH_MODULE_KEY + "-ESTATE-PROJECT-ACTIVITY-GOODS-ORDER";
	
	public static final String AUTH_QUERY = AUTH + "-QUERY";
	
	public static final String AUTH_GET = AUTH + "-GET";
	
	public static final String AUTH_CANCEL = AUTH + "-CANCEL";
	
	public static final String AUTH_AUDIT_QUERY = AUTH + "-AUDIT-QUERY";
	
	public static final String AUTH_AUDIT_GET = AUTH + "-AUDIT-GET";
	
	public static final String AUTH_AUDIT_PASSED = AUTH + "-REFUND-PASSED";
	
	public static final String AUTH_AUDIT_REFUDED = AUTH + "-REFUND-REFUDED";
	
	public static final String AUTH_EDIT_CUSTOMER = AUTH + "-EDIT-CUSTOMER";
	
	@Reference
	private EstateProjectActivityGoodsOrderService service;
	
	@Autowired
    private StorageService storageService;
	
	@Autowired
    private ConsumerMemberManager consumerMemberManager;
	
	@TypeField(type = EstateProjectActivityGoodsOrder.class,field = "operationUser",description = "操作用户")
	public ConsumerMember user(@Source EstateProjectActivityGoodsOrder source) {
		return consumerMemberManager.getByUserId(source.getOperationUserId());
	}
	
	@TypeField(type = EstateProjectActivityGoodsOrder.class,field = "paymentImage",description = "付款凭证")
	public StorageInfo coverImage(@Source EstateProjectActivityGoodsOrder source) {
		return storageService.get(source.getPaymentImage());
	}
	
	@TypeField(type = EstateProjectActivityGoodsOrderDetail.class, field = "coverImage", description = "地产项目商品订单明细商品图片")
    public StorageInfo coverImage(@Source EstateProjectActivityGoodsOrderDetail source) {
        return storageService.get(source.getCoverImage());
    }
	
	@TypeField(type = EstateProjectActivityGoodsOrderRefundAudit.class,field = "canAudit",description = "能操作审核")
	public boolean canAudit(@Source EstateProjectActivityGoodsOrderRefundAudit source,AdminMemberAuthority user) {
		return EstateProjectActivityGoodsOrderRefundAuditStatus.canAudit(source);
	}
	
	@TypeField(type = EstateProjectActivityGoodsOrder.class,field = "customer",description = "客户信息")
	public EstateProjectActivityGoodsOrderCustomer customer(@Source EstateProjectActivityGoodsOrder source) {
		return service.getCustomerByOrderId(source.getOrderId());
	}
	
	@TypeField(type = EstateProjectActivityGoodsOrder.class,field = "detailList",description = "订单明细列表")
	public List<EstateProjectActivityGoodsOrderDetail> detailList(@Source EstateProjectActivityGoodsOrder source){
		return service.detailList(source.getOrderId());
	}
	
	@QueryField(field = "estateProjectActivityGoodsOrderList" , description = "地产项目活动商品订单列表")
	@AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_QUERY, name = "地产项目活动商品订单列表")
	public Pagination<EstateProjectActivityGoodsOrder> query(Integer status,@NonNull Sorting[] sorts,@NonNull Page page,AdminMemberAuthority user){
		user.assertAuth(AUTH_QUERY);
		return service.query(status, sorts, page);
	}
	
	@QueryField(field = "estateProjectActivityGoodsOrder" , description = "地产项目活动商品订单详情")
	@AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_GET, name = "地产项目活动商品订单详情")
	public EstateProjectActivityGoodsOrder get(@NonNull Integer orderId,AdminMemberAuthority user){
		user.assertAuth(AUTH_GET);
		return service.get(orderId);
	}
	
	@MutationField(field = "cancelEstateProjectActivityGoodsOrder",description = "取消地产项目活动商品订单")
	@AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_CANCEL, name = "取消地产项目活动商品订单")
	public boolean cancelOrder(@NonNull Integer orderId,AdminMemberAuthority user) {
		user.assertAuth(AUTH_CANCEL);
		EstateProjectActivityGoodsOrder record = service.get(orderId);
		Assert.isTrue(Check.notNull(record),ResultCode.VERIFY.build(1), "活动商品订单不存在");
		service.cancelOrder(record, user.userName());
		return true;
	}
	
	@QueryField(field = "estateProjectActivityGoodsOrderRefundAudit" , description = "地产项目活动商品订单退款审核记录")
	@AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_AUDIT_GET, name = "地产项目活动商品订单退款审核记录")
	public EstateProjectActivityGoodsOrderRefundAudit getRefundAudit(@NonNull Integer auditId,AdminMemberAuthority user) {
		user.assertAuth(AUTH_AUDIT_GET);
		return service.getRefundAudit(auditId);
	}
	
	@QueryField(field = "estateProjectActivityGoodsOrderRefundAuditList" , description = "地产项目活动商品订单退款审核记录列表")
	@AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_AUDIT_QUERY, name = "地产项目活动商品订单退款审核记录列表")
	public Pagination<EstateProjectActivityGoodsOrderRefundAudit> getRefundAudit(Integer status,@NonNull Sorting[] sorts,@NonNull Page page,AdminMemberAuthority user) {
		user.assertAuth(AUTH_AUDIT_QUERY);
		return service.queryRefundAudit(status, sorts, page);
	}
	
	@MutationField(field = "estateProjectActivityGoodsOrderRefundAuditPassed",description = "地产项目活动商品订单退款审核通过")
	@AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_AUDIT_PASSED, name = "地产项目活动商品订单退款审核通过")
	public boolean refundAuditPassed(@NonNull Integer auditId,String remark,AdminMemberAuthority user) {
		user.assertAuth(AUTH_AUDIT_PASSED);
		service.auditPassed(auditId, remark, user.userId(), user.userName());
		return true;
	}
	
	@MutationField(field = "estateProjectActivityGoodsOrderRefundAuditRefused",description = "地产项目活动商品订单退款审核拒绝")
	@AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_AUDIT_REFUDED, name = "地产项目活动商品订单退款审核拒绝")
	public boolean refundAuditRefused(@NonNull Integer auditId,String remark,AdminMemberAuthority user) {
		user.assertAuth(AUTH_AUDIT_REFUDED);
		service.auditRefused(auditId, remark, user.userId(), user.userName());
		return true;
	}
	
	@MutationField(field = "editEstateProjectActivityGoodsOrderCustomer",description = "编辑地产项目活动商品订单客户信息")
	@AdminAuthoritativeRegister(parentKey = AUTH, key = AUTH_EDIT_CUSTOMER, name = "编辑地产项目活动商品订单客户信息")
	public EstateProjectActivityGoodsOrderCustomer editCustomer(@NonNull UpdateEstateProjectActivityGoodsOrderCustomerDTO record,AdminMemberAuthority user) {
		user.assertAuth(AUTH_EDIT_CUSTOMER);
		EstateProjectActivityGoodsOrder order = service.get(record.getOrderId());
		Assert.isTrue(Check.notNull(order), ResultCode.VERIFY.build(1),"活动商品订单不存在");
		return service.updateCustomer(record, user.userName());
	}
}
